PHP send an email with an attachment
Sending emails with attachments is a common task while developing PHP applications, achieved using PHP’s built-in mail()
function. The mail()
function allows developers to send emails with text or HTML content and attach one or more files.
Here’s a sample PHP code that demonstrates how to send an email with an attachment in PHP:
Example:
<?php
// recipient email address
$to = "[email protected]";
// subject of the email
$subject = "Email with Attachment";
// message body
$message = "This is a sample email with attachment.";
// from
$from = "[email protected]";
// boundary
$boundary = uniqid();
// header information
$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\".$boundary.\"\r\n";
// attachment
$attachment = chunk_split(base64_encode(file_get_contents('file.pdf')));
// message with attachment
$message = "--".$boundary."\r\n";
$message .= "Content-Type: text/plain; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n\r\n";
$message .= chunk_split(base64_encode($message));
$message .= "--".$boundary."\r\n";
$message .= "Content-Type: application/octet-stream; name=\"file.pdf\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment; filename=\"file.pdf\"\r\n\r\n";
$message .= $attachment."\r\n";
$message .= "--".$boundary."--";
// send email
if (mail($to, $subject, $message, $headers)) {
echo "Email with attachment sent successfully.";
} else {
echo "Failed to send email with attachment.";
}
?>
The recipient’s email address, subject, message body, and header information are defined in the code above. The boundary variable separates the message and attachment in the email.
The attachment is read from a file using file_get_contents()
and encoded using base64_encode()
. The chunk_split()
function splits the attachment into smaller chunks for sending.
Finally, the mail()
function sends the email with an attachment. If the email has been sent successfully, the function will return true
; Otherwise, it will return false
.
Send HTML form data over email using PHP
To get the HTML form content and send it in an email, you need to modify the code as follows:
- Add an HTML form to your webpage to collect the desired information from users.
- When submitting the HTML form, use PHP code to retrieve the form data and store it in variables.
- In the above PHP code, replace the hardcoded values of the variables with the input names of the form data.
Here’s an example HTML code for a form that allows you to submit an email with the recipient’s email address, subject, message, sender’s email address, and an attachment:
Example:
<form action="send-email.php" method="post" enctype="multipart/form-data">
<label for="to">To:</label>
<input type="email" id="to" name="to"><br>
<label for="subject">Subject:</label>
<input type="text" id="subject" name="subject"><br>
<label for="message">Message:</label>
<textarea id="message" name="message"></textarea><br>
<label for="from">From:</label>
<input type="email" id="from" name="from"><br>
<label for="attachment">Attachment:</label>
<input type="file" id="attachment" name="attachment"><br>
<input type="submit" value="Send">
</form>
In the code above, the form’s action is set to “send-email.php”, where the form data will be sent for processing. The enctype
attribute is set to multipart/form-data
to indicate that the form will contain both text and binary data (i.e., the file attachment). The input fields allow you to enter the recipient email, subject, message, sender email and select a file for attachment. The submit button is labeled “Send”.
Below is a code of the “send-email.php” page, which receives data and sends emails using the above HTML form:
Example:
<?php
// recipient email address
$to = $_POST['to'];
// subject of the email
$subject = $_POST['subject'];
// message body
$message = $_POST['message'];
// from
$from = $_POST['from'];
// boundary
$boundary = uniqid();
// header information
$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\".$boundary.\"\r\n";
// attachment
$file = $_FILES["attachment"]["tmp_name"];
$filename = $_FILES["attachment"]["name"];
$attachment = chunk_split(base64_encode(file_get_contents($file)));
// message with attachment
$message = "--".$boundary."\r\n";
$message .= "Content-Type: text/plain; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n\r\n";
$message .= chunk_split(base64_encode($message));
$message .= "--".$boundary."\r\n";
$message .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$message .= $attachment."\r\n";
$message .= "--".$boundary."--";
// send email
if (mail($to, $subject, $message, $headers)) {
echo "Email with attachment sent successfully.";
} else {
echo "Failed to send email with attachment.";
}
?>
In conclusion, sending an email with attachments in PHP is a straightforward process that can be achieved using the mail()
function and format the email correctly.
Very nice information found in your website, I hope this kind of information is always available, I hope that many people will find information through your website.
This site would appeal to the average person in the way that it is
very simply done, and everything is very easy to find. There is no
messing around here, just the information that they want to convey
presented in the simplest way possible. People with no technical back
ground would definitely appreciate the simplicity and ease of
navigation in this site.
The layout is very clean, but also kind of bland. It makes you feel
comfortable with the site because you don’t feel overwhelmed by
information, but the presentation of the site needs to be worked on a
bit more.
It is extremely easy to navigate this site because all of the links
are right there on the left, which is excellent. One thing with the
navigation is you should have an alternative form of getting around.
If someone has a browser which doesn’t load graphics, it would be
difficult for them to get around this site, so you should have the
links in text form at the bottom of the page. Other than that, the
navigation is perfect!